package com.cloud.commons.utils;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateUtils {

    /**
     * 获取当天凌晨时间戳
     * @return
     */
    public static Long getCurrentDayStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Long start = calendar.getTimeInMillis();
        return start;
    }

    /**
     * 获取当天23点时间戳
     * @return
     */
    public static Long getCurrentDayEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);
        Long endTime = calendar.getTimeInMillis();
        return endTime;
    }

    /**
     *  获取当前年-月份
     */
    public static String getYearAndMonth(Long valueTime) {
        if (ObjectUtils.isEmpty(valueTime)) {
            throw new RuntimeException("获取不到有效时间：" + valueTime);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(valueTime);
        return calendar.get(Calendar.YEAR) +"/"+ calendar.get(Calendar.MONTH) + 1;
    }


    /**
     * 获取指定日期当前月-开始时间
     */
    public static Long getCurrentMonthFirstDay(Long valueTime){
        if (ObjectUtils.isEmpty(valueTime)) {
            throw new RuntimeException("获取不到有效时间" + valueTime);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(valueTime);
        calendar.set(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获得当前月--结束日期
     *
     * @return
     */
    public static Long getCurrentMonthLastDay(Long valueTime){
        if (ObjectUtils.isEmpty(valueTime)) {
            throw new RuntimeException("获取不到有效的结束时间：" + valueTime);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.MONTH,1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        //将时、分、秒、毫秒 清零
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTimeInMillis();
    }


    // 日期转字符串格式
    public static String timeStamp2Date(String seconds, String format) {
        if (StringUtils.isEmpty(seconds) || StringUtils.isEmpty(format)) {

        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    /**
     * 获取给定时间 当天0点的时间戳
     *
     * @param time
     * @return
     */
    public static Long getDayStartTime(Long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(time));

        //将时、分、秒、毫秒 清零
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime().getTime();
    }

    /**
     * 获取给定时间 当天24点的时间戳
     *
     * @param time
     * @return
     */
    public static Long getDayEndTime(Long time) {
        Long startTime = getDayStartTime(time);
        Long oneDayTimeMillis = 60000 * 60 * 24L;
        return startTime + oneDayTimeMillis;
    }



    /**
     * 获取当前日期是星期几
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    /**
     * 指定某个月份返回其月份所有的天数
     * @param valueMonth
     */
    public static List<Long> getDayByValueMonth(Long valueMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(valueMonth);

        // 对应月份
        Integer month = calendar.get(Calendar.MONTH) + 1;
        // 对应年份
        Integer year = calendar.get(Calendar.YEAR);
        // 获取当前月份格式化
        Long formatMonth = getCurrentMonthFirstDay(valueMonth);
        // 保存的数据
        List<Long> currentDayList = new ArrayList<>();
        currentDayList.add(formatMonth);
        // 对应天数
        Integer valueDay = 0;
        switch (month) {
            case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                valueDay = 31;
                break;
            case 4: case 6: case 9: case 11:
                valueDay = 30;
                break;
            case 2:
                valueDay = (year % 400) == 0 || ((year%4)==0)&&((year%100)!=0) ? 29 : 28;
                break;
        }
        for (int i = 1; i<= valueDay - 1; i++) {
            formatMonth += 86400000;
            currentDayList.add(formatMonth);
        }
        return currentDayList;
    }


    public static void main(String[] args) {
        System.out.println(getDayByValueMonth(System.currentTimeMillis()));
    }


}
